package cn.bj.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.bj.beans.Admin;
import cn.bj.beans.AdminExample;
import cn.bj.beans.AdminExample.Criteria;
import cn.bj.dao.AdminMapper;

@Service
public class AdminService {

	@Autowired
	AdminMapper adminMapper;

	// 验证登录请求
	public Admin validate(String username, String password) {
		// 通过criteria构造查询条件
		AdminExample adminExample = new AdminExample();
		// 数据库字段 asc升序,desc降序排列
		adminExample.setOrderByClause("username asc");
		// 去除重复,true是选择不重复记录,false反之
		adminExample.setDistinct(false);
		// 构造自定义查询条件，把方法参数的username传入
		Criteria criteria = adminExample.createCriteria();
		criteria.andUsernameEqualTo(username);

		// 请求dao层接口方法，对应mapper.xml的sql语句
		List<Admin> admins = adminMapper.selectByExample(adminExample);
		System.out.println("名字-----" + username);
		// 对比查询出来的数据，进行密码比对
		if (null != admins) {
			for (Admin admin : admins) {
				System.out.println("密码--------" + password);
				if (admin.getPassword().equals(password)) {
					// 找到即返回
					return admin;
				}
			}

		}
		// 不然返空
		return null;
	}

	// 查询所有
	public List<Admin> getAllAdmins(Object object) {
		return adminMapper.selectByExample(null);
	}

	// 删除
	public int deleteAdmin(int id) {

		return adminMapper.deleteByPrimaryKey(id);
	}

	// 添加
	public int insertAdmins(Admin admin) {
		return adminMapper.insert(admin);
	}

	// id查询
	public Admin selectAdminById(Integer id) {
		return adminMapper.selectByPrimaryKey(id);
	}

	// 修改
	public int updateAdmin(Admin admin) {

		return adminMapper.updateByPrimaryKey(admin);
	}

}
